<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="配件名称" prop="accessoriesId">
        <el-select v-model="form.accessoriesId"  clearable placeholder="请选择">
          <el-option
            v-for="item in optionss"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="维修保养描述" prop="storeId">
        <el-select v-model="form.recordId" clearable placehol-der="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.des"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="领料数量" prop="count">
        <el-input v-model="form.count" />
      </el-form-item>

      <el-form-item label="指派时间" prop="useTime">
        <el-date-picker v-model="form.useTime" type="data" style="width: 100%" />
      </el-form-item>

    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">添加</el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
// eslint-disable-next-line import/no-duplicates
import { save, modify } from '../../../api/picking'
import { listSupply } from '../../../api/accessories'
import { ElMessage } from 'element-plus'
// eslint-disable-next-line import/no-duplicates
import { listAll } from '../../../api/maintainRecord'

const form = ref({
  id: '',
  accessoriesId: '',
  recordId: '',
  count: '',
  createTime: '',
  useTime: ''
})

const options = ref([])
const optionss = ref([])
async function query3() {
  // 维修保养
  const row = await listAll()
  options.value = row.data.data
  // 配件名称
  const rows = await listSupply()
  optionss.value = rows.data.data
}

// vue组件加载完成后执行
onMounted(() => {
  query3()
})

const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    // console.log(props.dialogRowValue)
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(true)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 表单校验
const rules = ref({
  accessoriesId: [
    { required: true, message: '描述不能为空' }
  ],
  recordId: [
    { required: true, message: '选择配件不能为空' }
  ],
  materialCount: [
    { required: true, message: '数量不能为空' }
    // { type: 'number', message: '数量必须是数字' }
  ]
})

const emits = defineEmits(['update:modelValue', 'query'])

// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确认
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加' ? await save(form.value) : await modify(form.value)
      ElMessage({
        message: '更新成功',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
